#!/usr/bin/python
#coding:utf-8
'''
图像显示
'''

import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] #支持中文
import random
import numpy as np

def showColumn(data,title):
    '''
    柱状图
    '''
    keyList={}
    for one in data:
        if one in keyList:
            value =keyList.get(one)
            value+=1
        else:
            value=1
        keyList[one]=value
    
    
    lab=[]
    sizes=[]
    for key in keyList:
        lab.append(key)
        sizes.append(keyList.get(key))

    plt.bar(range(len(sizes)),sizes,color='rgb',tick_label=lab) 
    # 设置数量
    num=0
    for xx,yy in zip(lab,sizes):  #填写内容
        # plt.text(xx, yy+0.1, str(yy), ha='center')  #前两位坐标，第三位名字，第四位文本显示格式
        plt.text(num, yy+0.1, str(yy), ha='center')  #前两位坐标，第三位名字，第四位文本显示格式
        num+=1
    plt.title(title)
    plt.show();


def showLine(data,title,firstColumnState):
    '''
    多线折线图显示
    args:
        data:数据
        title：标题
        firstColumnState:第一列状态 -1：展示，0：不展示，1：横坐标
    '''
    
    firstColumn=[]
    if firstColumnState ==1:   #第一列为横坐标
        firstColumn=data[data.keys[0]]
        firstColumn=np.array(firstColumn)

    for i,row in enumerate(data):
        if firstColumnState!=-1 and i==0:  #不展示第一列
            continue;

        value=data.get(row)
        value=np.array(value)
        color=randomcolor()
        label=row+" 总数:"+str(np.size(value))+" 均值:"+str(round(np.mean(value),3))+" 最大值:"+str(np.max(value))+" 最小值:"+str(np.min(value))
        if len(firstColumn)>0:
            plt.plot(firstColumn,value,c=color,label=label)
        else:
            plt.plot(value,c=color,label=label)

    plt.xticks(rotation=45)  #旋转x轴刻度
    plt.title(title)
    plt.xlabel('time')
    plt.ylabel("value")
    plt.legend(loc='upper left')
    plt.show()

def randomcolor():
    colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color = ""
    for i in range(6):
        color += colorArr[random.randint(0,14)]
    return "#"+color